AutoCloseWindow false
ShowInstDetails show
ShowUnInstDetails hide
SetOverwrite try
SetCompress auto
SetDateSave on

; HM NIS Edit Wizard helper defines
!define PRODUCT_NAME "Congress Explorer"
!define PRODUCT_VERSION "1.0.0"
!define PRODUCT_PUBLISHER "ABSo"
!define PRODUCT_WEB_SITE "http://abso.freehostia.com"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME} ${PRODUCT_VERSION}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

;MUI Settings
!define MUI_INSTFILESPAGE_COLORS /windows
!define MUI_INSTFILESPAGE_PROGRESSBAR "smooth"
!define MUI_FINISHPAGE_NOAUTOCLOSE
!define MUI_UNFINISHPAGE_NOAUTOCLOSE
!define MUI_WELCOMEFINISHPAGE_BITMAP ce.bmp
!define MUI_UNWELCOMEFINISHPAGE_BITMAP ce-uninst.bmp
!define MUI_ABORTWARNING
!define MUI_ICON ce.ico
!define MUI_UNICON uninst.ico
!define MUI_COMPONENTSPAGE
!define MUI_COMPONENTSPAGE_NODESC

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "Temp~\${PRODUCT_NAME}_${PRODUCT_VERSION}.Setup.exe"

!include "MUI.nsh"
!include "Sections.nsh"

;Variables
Var JAVA_HOME
Var JAVA_VER
Var JAVA_INSTALLATION_MSG
  
; Welcome page
!insertmacro MUI_PAGE_WELCOME

; Options page
!insertmacro MUI_PAGE_COMPONENTS

; Install directory page
!define MUI_PAGE_CUSTOMFUNCTION_PRE PrepareInstDir
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE CheckEmptyInstDir
!insertmacro MUI_PAGE_DIRECTORY

; JDK selection page
!define MUI_DIRECTORYPAGE_VARIABLE $JAVA_HOME
!define MUI_PAGE_HEADER_TEXT "Choose JDK path"
!define MUI_PAGE_HEADER_SUBTEXT "Choose the folder in which your Java Development Kit is located."
!define MUI_DIRECTORYPAGE_TEXT_TOP "Setup has detected the following Java Development Kit installation path. If you want to use a JDK located in a different folder, you can change it using Browse botton. Click Install to finish the installation."
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION "JDK Path."
!define MUI_PAGE_CUSTOMFUNCTION_PRE JavaHomePre
!insertmacro MUI_PAGE_DIRECTORY

; Instfiles page
!insertmacro MUI_PAGE_INSTFILES

; Finish page
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_TEXT  "Run ${PRODUCT_NAME}"
!define MUI_FINISHPAGE_RUN_FUNCTION "LaunchLink"
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English"

Section -Pre
  SetOutPath "$INSTDIR"
  File ce.ico ;do not remove: the win registry references this file to show the appropriate icon in control panel
SectionEnd

Section "!Congress Explorer 1.0.0"
  SectionIn RO
  SetOutPath "$INSTDIR"
  File /r "Temp~\build\*.*"
SectionEnd

Section -StartMenu
  WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
  CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
  CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_VERSION}"
  CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_VERSION}\${PRODUCT_NAME}.lnk" "$INSTDIR\CongressExplorer.exe" '-vm "$JAVA_HOME\bin\javaw.exe"' "$INSTDIR\ce.ico"
  CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_VERSION}\Uninstall ${PRODUCT_NAME}.lnk" "$INSTDIR\uninst.exe"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\ce.ico"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd

Section Uninstall
  RMDir /r "$INSTDIR"
  RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_VERSION}"
  RMDir "$SMPROGRAMS\${PRODUCT_NAME}"  ; removes only if empty
  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  SetAutoClose false
SectionEnd

;Install Functions
Function PrepareInstDir
  StrCpy $INSTDIR "$PROGRAMFILES\${PRODUCT_NAME}\${PRODUCT_VERSION}"
FunctionEnd

;Java Home Pre-function
Function JavaHomePre
  Call LocateJavaHome
  StrCmp "" $JAVA_INSTALLATION_MSG Success
  MessageBox MB_OK|MB_ICONSTOP "$JAVA_INSTALLATION_MSG"
 Success:
FunctionEnd

;Uninstall Functions
Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove $(^Name) and all of its components?" IDYES +2
  Abort
FunctionEnd

Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer."
FunctionEnd

; Utility Functions
Function CheckEmptyInstDir
  Push $INSTDIR
  Call RemoveEndingBackSlash
  Pop $INSTDIR
  IfFileExists $INSTDIR\uninst.exe 0 +3
  MessageBox MB_OK|MB_ICONSTOP "The installation directory is not empty. Please select a new directory for installation."
  Abort
FunctionEnd

Function RemoveEndingBackSlash
  Exch $9
  Push $8
  Push $7
  StrLen $7 $9
  IntOp $7 $7 - 1
  StrCpy $8 $9 1 -1
  StrCmp $8 "\" 0 exit
  StrCpy $9 $9 $7
exit:
  Pop $7
  Pop $8
  Exch $9
FunctionEnd

Function LocateJavaHome
  ;Check for Java version and location
  Push $0
  Push $1
  ReadRegStr $JAVA_VER HKLM "SOFTWARE\JavaSoft\Java Development Kit" CurrentVersion
  StrCmp "" "$JAVA_VER" JavaNotPresent CheckJavaVer
 JavaNotPresent:
  StrCpy $JAVA_INSTALLATION_MSG "Java Development Kit is not installed on your computer. You need version 1.5 or higher to run ${PRODUCT_NAME}."
  Goto Done
 CheckJavaVer:
  ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Development Kit\$JAVA_VER" JavaHome
  GetFullPathName $JAVA_HOME "$0"
  StrCpy $0 $JAVA_VER 1 0
  StrCpy $1 $JAVA_VER 1 2
  StrCpy $JAVA_VER "$0$1"
  IntCmp 15 $JAVA_VER FoundCorrectJavaVer FoundCorrectJavaVer JavaVerNotCorrect
 FoundCorrectJavaVer:
  IfFileExists "$JAVA_HOME\bin\javaw.exe" 0 JavaNotPresent
  Goto Done
 JavaVerNotCorrect:
  StrCpy $JAVA_INSTALLATION_MSG "The version of the default Java Development Kit installed on your computer is $JAVA_VER. Version 1.5 or newer is required to run ${PRODUCT_NAME}."
 Done:
  Pop $1
  Pop $0
FunctionEnd

Function LaunchLink
  ExecShell "" "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_VERSION}\${PRODUCT_NAME}.lnk"
FunctionEnd
